/**
 * Licensed to Open-Ones Group under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Open-Ones Group licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package vn.fpt.fsoft.coreframework;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


/**
 * @author DoN
 *
 */
public class Board {

    private int width;
    private int height;
    private char[][] baseLayer;

    /**
     * @return the width
     */
    public int getWidth() {
        return width;
    }

    /**
     * @param width the width to set
     */
    public void setWidth(int width) {
        this.width = width;
    }

    /**
     * @return the height
     */
    public int getHeight() {
        return height;
    }

    /**
     * @param height the height to set
     */
    public void setHeight(int height) {
        this.height = height;
    }

    /**
     * @return the baseLayer
     */
    public char[][] getBaseLayer() {
        return baseLayer;
    }

    /**
     * @param baseLayer the baseLayer to set
     */
    public void setBaseLayer(char[][] baseLayer) {
        this.baseLayer = baseLayer;
    }

    public void loadMap(String nameMap) {
        // Map map = new Map();

        try {
            BufferedReader buffer = new BufferedReader(new FileReader(nameMap));
            String str = buffer.readLine();
            int width = Integer.valueOf(str.split(" ")[0]);
            int height = Integer.valueOf(str.split(" ")[1]);
            this.setWidth(width);
            this.setHeight(height);
            baseLayer = new char[height][width];
            String l = null;
            int index = 0;
            while ((l = buffer.readLine()) != null) {
                for (int i = 0; i < l.length(); i++) {
                    // if(l.charAt(i) == ' '){
                    // System.out.println((int)l.charAt(i));
                    // }
                    this.baseLayer[index][i] = l.charAt(i);
                    System.out.println((int) l.charAt(i));
                }
                index++;

            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // for (int i = 0; i< ))

        // return map;
    }

    public char getBaseLayerValue(Point mapPosition) {
        return baseLayer[mapPosition.x][mapPosition.y];
    }

    public Rectangle getBaseLayerSourceRectangle(Point mapPosition) {
        char baseLayerValue = getBaseLayerValue(mapPosition);
        if (baseLayerValue < 0) {
            return new Rectangle();
        }

        if (baseLayerValue == ' ') {
           return new Rectangle(0, 0, 32, 32);
        } else if (baseLayerValue == 'S') {
        	 return new Rectangle(64, 0, 32, 32);
        } else if (baseLayerValue == 'P') {
        	 return new Rectangle(32, 0, 32, 32);
        }
        return new Rectangle(98, 0, 32, 32);
//        return new Rectangle(((baseLayerValue - 64) % 10) * 32, ((baseLayerValue - 64) / 10) * 32, 32, 32);
    }

    public void drawBackGround(Graphics2D g) {
        Sprite sprite = new Sprite();
        BufferedImage image = sprite.getSprite("images/titles.png");
        // g.drawImage(image, 0, 0, 64, 64, 64, 0, 128, 64, null);
        for (int x = 0; x < height; x++) {
            for (int y = 0; y < width; y++) {
                Rectangle destinationRectangle = new Rectangle(x * 32, y * 32, 32, 32);
                {
                    Point mapPosition = new Point(x, y);
                    // Sprite sprite = new Sprite();
                    // BufferedImage image = sprite.getSprite("images/DefaultMap.png");
                    Rectangle sourceRectangle = getBaseLayerSourceRectangle(mapPosition);
                    if (sourceRectangle != new Rectangle()) {
                        // g.draw3DRect(destinationRectangle.x,destinationRectangle.y,destinationRectangle.width,destinationRectangle.height,
                        // true);
                        g.drawImage(image, destinationRectangle.x, destinationRectangle.y, destinationRectangle.x
                                + destinationRectangle.width, destinationRectangle.y + destinationRectangle.height,
                                sourceRectangle.x, sourceRectangle.y, sourceRectangle.x + sourceRectangle.width,
                                sourceRectangle.y + sourceRectangle.height, null);
                    }
                }
            }
        }
    }
}
